package flappy.Game;

import java.util.*;
import com.badlogic.gdx.*;
import com.badlogic.gdx.audio.*;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.*;

public class TaiNguyen {
	public static final int    KhoangCach2DauOng      = 50; // 45
	public static final int    TocDoThanhCuon         = -70; // tốc độ cuộn của nền
	public static final int    KhoangCachGiua2OngNuoc = 55; // khoảng cách giữa 2 ống nước
	public static final int    DoRongDauOng           = 26;
	public static final int    DoDaiDauOng            = 14;
	public static final int    TrongLuc               = 500;
	public static final int    LucKichThich           = 150;
	
	private static Preferences boNho;
	static Random              Random;
	public static Texture      texture;
	public static BitmapFont   fontDiemDaChoi, fontDiemXepHang;
	public static TextureRegion taptap, flappyBird, canhVat, matDat, chim, chim2, chim3, dauOngNuoc, dauOngNuocNguoc, ongNuoc, ready, gameOver, bangDiem;
	public static Animation     dsHinhChim;
	public static Sound         amThanhVaCham, amThanhVoCanh, amThanhAnDiem, amThanhChet, amThanhChaoMung;
	
	static {
		Random = new Random();
		texture = new Texture(Gdx.files.internal("data/sprites.png"));
		ready = new TextureRegion(texture, 295, 59, 92, 25);
		ready.flip(false, true);
		
		gameOver = new TextureRegion(texture, 395, 59, 96, 21);
		gameOver.flip(false, true);
		
		bangDiem = new TextureRegion(texture, 3, 259, 113, 57);
		bangDiem.flip(false, true);
		
		flappyBird = new TextureRegion(texture, 351, 91, 89, 24);
		flappyBird.flip(false, true);
		
		taptap = new TextureRegion(texture, 292, 91, 57, 49);
		taptap.flip(false, true);
		
		matDat = new TextureRegion(texture, 292, 0, 168, 56);
		matDat.flip(false, true);
		
		amThanhVaCham = Gdx.audio.newSound(Gdx.files.internal("data/hit.wav"));
		amThanhVoCanh = Gdx.audio.newSound(Gdx.files.internal("data/wing.wav"));
		amThanhAnDiem = Gdx.audio.newSound(Gdx.files.internal("data/point.wav"));
		amThanhChet = Gdx.audio.newSound(Gdx.files.internal("data/die.wav"));
		amThanhChaoMung = Gdx.audio.newSound(Gdx.files.internal("data/swooshing.wav"));
		fontDiemDaChoi = new BitmapFont(Gdx.files.internal("data/flappyfont.fnt"));
		fontDiemDaChoi.setScale(.5f, -.5f);
		
		fontDiemXepHang = new BitmapFont(Gdx.files.internal("data/flappyfont.fnt"));
		fontDiemXepHang.setScale(.25f, -.25f);
		
		boNho = Gdx.app.getPreferences("FlappyBird");
	}
	
	public static void KhoiTaoNgauNhien() {
		KhoiTaoCanhVat();
		KhoiTaoChim();
		KhoiTaoOngNuoc();
	}
	
	private static void KhoiTaoCanhVat() {
		int v = Random.nextInt(2);
		
		if (v % 2 == 0) canhVat = new TextureRegion(texture, 0, 0, 144, 256);
		else canhVat = new TextureRegion(texture, 146, 0, 144, 256);
		canhVat.flip(false, true);
	}
	
	private static void KhoiTaoOngNuoc() {
		int v = Random.nextInt(4);
		System.out.println(v);
		if (v < 3) {
			dauOngNuoc = new TextureRegion(texture, 84, 323, 26, 14);
			ongNuoc = new TextureRegion(texture, 85, 360, 24, 3);
		} else {
			dauOngNuoc = new TextureRegion(texture, 28, 323, 26, 14);
			ongNuoc = new TextureRegion(texture, 29, 360, 24, 3);
		}
		dauOngNuocNguoc = new TextureRegion(dauOngNuoc);
		dauOngNuocNguoc.flip(false, true);
	}
	
	private static void KhoiTaoChim() {
		int v = Random.nextInt(3);
		
		if (v % 3 == 0) {
			chim2 = new TextureRegion(texture, 59, 491, 17, 12);
			chim = new TextureRegion(texture, 31, 491, 17, 12);
			chim3 = new TextureRegion(texture, 3, 491, 17, 12);
		}
		
		else if (v % 2 == 1) {
			chim2 = new TextureRegion(texture, 115, 329, 17, 12);
			chim = new TextureRegion(texture, 115, 355, 17, 12);
			chim3 = new TextureRegion(texture, 87, 491, 17, 12);
		}
		
		else {
			chim2 = new TextureRegion(texture, 115, 381, 17, 12);
			chim = new TextureRegion(texture, 115, 407, 17, 12);
			chim3 = new TextureRegion(texture, 115, 433, 17, 12);
		}
		
		chim2.flip(false, true);
		chim.flip(false, true);
		chim3.flip(false, true);
		TextureRegion[] dsChim = { chim, chim2, chim3 };
		dsHinhChim = new Animation(0.06f, dsChim);
		dsHinhChim.setPlayMode(Animation.LOOP_PINGPONG);
	}
	
	public static void CapNhatDiem(int val) {
		boNho.putInteger("DiemCaoNhat", val);
		boNho.flush();
	}
	
	public static int LayDiemCaoNhat() {
		if (!boNho.contains("DiemCaoNhat")) {
			boNho.putInteger("DiemCaoNhat", 0);
			return 0;
		}
		return boNho.getInteger("DiemCaoNhat");
	}
}